
//
//  personalView.m
//  ;
//
//  Created by tangtangchitangtang on 2017/11/8.
//  Copyright © 2017年 tangtangchitangtang. All rights reserved.
//

#import "personalView.h"
#import "accountViewController.h"
#import "goodsReceiptViewController.h"
@implementation personalView
-(void)creatView{
    NSDictionary *dic = [NSKeyedUnarchiver unarchiveObjectWithFile:_userModelFile];
    
    _dic=[NSMutableDictionary dictionaryWithDictionary:dic];
    
    self.backgroundColor=_LightGrey;
    [self creatTopView];
    [self creatFunctionView];
    
    UIButton *btn=[UIButton new];
    [self addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(50);
    }];
    btn.backgroundColor=_tianLanColor;
    [btn setTitle:@"退出登录" forState:0];
    [btn setTitleColor:[UIColor whiteColor] forState:0];
    btn.titleLabel.font=[UIFont systemFontOfSize:16];
    [btn addTarget:self action:@selector(tuiChuBtn:) forControlEvents:UIControlEventTouchUpInside];
}

-(void)creatTopView{
    UIView *topView=[UIView new];
    [self addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(64);
        make.height.mas_equalTo(111);
    }];
    topView.backgroundColor=[UIColor whiteColor];
    
    UIView *imageview=[UIView new];
    [topView addSubview:imageview];
    [imageview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(0);
        make.height.mas_equalTo(60);
    }];
    imageview.userInteractionEnabled=YES;
    UITapGestureRecognizer *tapGesture2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(userIconChange)];
    [imageview addGestureRecognizer:tapGesture2];
    
    UILabel *imageLabel=[UILabel new];
    [imageview addSubview:imageLabel];
    [imageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.centerY.mas_equalTo(0);
    }];
    imageLabel.text=@"头像";
    imageLabel.textColor=ColorWithRGB(49, 49, 49);
    imageLabel.font=[UIFont systemFontOfSize:18];
    
    _imageView=[UIImageView new];
    [imageview addSubview:_imageView];
    [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-25);
        make.centerY.mas_equalTo(0);
        make.width.height.mas_equalTo(45);
    }];
    _imageView.layer.masksToBounds=YES;
    _imageView.layer.cornerRadius=45/2;
    NSString *headImg=[_dic objectForKey:@"headImg"];
    NSURL *imageUrl=[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",_addressUrl2,headImg]];
    [_imageView sd_setImageWithURL:imageUrl];

    
    
    UIView *xianView=[UIView new];
    [topView addSubview:xianView];
    [xianView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(1);
        make.top.mas_equalTo(imageview.mas_bottom);
    }];
    xianView.backgroundColor=_LightGrey;
    
    UIView *nameView=[UIView new];
    [topView addSubview:nameView];
    [nameView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(50);
    }];
    
    UILabel *nameLable=[UILabel new];
    [nameView addSubview:nameLable];
    [nameLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.centerY.mas_equalTo(0);
    }];
    nameLable.text=@"昵称";
    nameLable.textColor=ColorWithRGB(49, 49, 49);
    nameLable.font=[UIFont systemFontOfSize:18];
    
    UILabel *namelbale2=[UILabel new];
    [nameView addSubview:namelbale2];
    [namelbale2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-10);
        make.centerY.mas_equalTo(0);
    }];
    namelbale2.text=[_dic objectForKey:@"nickName"];
    namelbale2.textColor=ColorWithRGB(150, 150, 150);
    namelbale2.font=[UIFont systemFontOfSize:15];
   
}

-(void)creatFunctionView{
    
    UIView *functionView=[UIView new];
    [self addSubview:functionView];
    [functionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(185);
        make.bottom.mas_equalTo(0);
    }];
    functionView.backgroundColor=[UIColor whiteColor];
    
    NSArray *nameArr=[NSArray arrayWithObjects:@"收货地址",@"账户安全",@"音效于通知",@"客户服务",@"关于我们", nil];
    for (int i=0; i<5; i++) {
        UIView *view=[UIView new];
        [functionView addSubview:view];
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(0);
            make.height.mas_equalTo(50);
            make.top.mas_equalTo(51*i);
        }];
        view.tag=i;
        view.userInteractionEnabled=YES;
        UITapGestureRecognizer *pushToOther = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushToOher:)];
        [view addGestureRecognizer:pushToOther];
        
        UILabel *label=[UILabel new];
        [view addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(15);
            make.centerY.mas_equalTo(0);
        }];
        label.text=nameArr[i];
        label.textColor=ColorWithRGB(49, 49, 49);
        label.font=[UIFont systemFontOfSize:18];
       
        UIView *xianView=[UIView new];
        [functionView addSubview:xianView];
        [xianView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(15);
            make.right.mas_equalTo(10);
            make.height.mas_equalTo(1);
            make.top.mas_equalTo(view.mas_bottom);
        }];
        xianView.backgroundColor=_LightGrey;
      
    }
    
}

-(IBAction)tuiChuBtn:(id)sender{

    
    _alert=[alertView new];
    [self.window addSubview:_alert];
    [_alert mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.mas_equalTo(0);
    }];
    [_alert creatView];
    [_alert.okBtn addTarget:self action:@selector(tuiChuBtn2:) forControlEvents:UIControlEventTouchUpInside];

}
-(IBAction)tuiChuBtn2:(id)sender{
    [_alert removeFromSuperview];
    NSFileManager *defaultManager = [NSFileManager defaultManager];
    [defaultManager removeItemAtPath:_userModelFile error:nil];
    [self.viewController.navigationController popViewControllerAnimated:YES];

}
-(void)pushToOher:(UITapGestureRecognizer *)tap{
    if (tap.view.tag==0) {
        goodsReceiptViewController *account=[[goodsReceiptViewController alloc] init];
        [ZQTools pushNextViewController:self.viewController andRootController:account];
    }
    if (tap.view.tag==1) {
        accountViewController *account=[[accountViewController alloc] init];
        [ZQTools pushNextViewController:self.viewController andRootController:account];
    }
    
    
}

-(void) userIconChange{
    UIAlertController *actionSheetController = [UIAlertController alertControllerWithTitle: @"" message: @"拍照或从相册中选择" preferredStyle: UIAlertControllerStyleActionSheet];
    
    [actionSheetController addAction: [UIAlertAction actionWithTitle:@"打开相册"style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        
        [self LocalPhoto];
        
    }]];
    [actionSheetController addAction: [UIAlertAction actionWithTitle: @"拍照" style: UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {
        [self locaXiangJi];
        
        
    }]];
    
    [actionSheetController addAction: [UIAlertAction actionWithTitle:@"取消"style:UIAlertActionStyleCancel handler:^(UIAlertAction *_Nonnull action) {}]];
    
    [self.viewController presentViewController: actionSheetController animated:YES completion:^{}];
    
}

-(void)locaXiangJi{
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    
    UIImagePickerControllerQualityType qualityType = UIImagePickerControllerQualityTypeHigh;
    
    /*判断是否有相机*/
    
    if([UIImagePickerController isSourceTypeAvailable : UIImagePickerControllerSourceTypeCamera]){
        
        UIImagePickerController *picker = [[UIImagePickerController alloc]init];
        
        picker.delegate=self;
        
        /*设置拍照后的图片可被编辑*/
        
        picker.allowsEditing=YES;
        
        /*资源类型为照相机*/
        
        picker.sourceType= sourceType;
        
        /*品质为高*/
        
        picker.videoQuality= qualityType;
        
        [self.viewController presentViewController: picker animated:YES completion:^{
            
            
        }];
        
    }else{
        //        [self alertTitle:@"该设备无摄像头"];
        
    }
    
}
-(void)LocalPhoto
{
    
    UIImagePickerController *picker = [[UIImagePickerController alloc]init];
    
    /*资源类型为图片库*/
    
    picker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
    
    picker.delegate=self;
    
    /*设置选择后的图片可被编辑*/
    
    picker.allowsEditing=YES;
    
    [self.viewController presentViewController:picker animated:YES completion:^{
        
        
    }];
    
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    
    
    
    [picker dismissViewControllerAnimated:YES completion:^{

        UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
       

        NSArray *imageArr=[NSArray arrayWithObjects:image, nil];
        
        [ZQTools afnPostImageWithDict:nil WithAddressUrl:nil withImageArr:imageArr withFileName:@"file" WithView:self success:^(id responseObject) {
            
            NSDictionary *dic=[responseObject objectForKey:@"data"];
            NSString *imageStr=[NSString stringWithFormat:@"%@%@",[dic objectForKey:@"uploadfilepath"],[dic objectForKey:@"uploadfilename"]];
            [self updateUserImage:imageStr imagge:image];
        }];
    
    }];
}

-(void)updateUserImage:(NSString *)imageStr imagge:(UIImage *)image{
    
    
    NSString *token=[_dic objectForKey:@"id"];

    NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys:imageStr,@"headImg",token,@"id", nil];
    [ZQTools AFNPostDataUrl:@"suser/updateUser" Dict:dict andTableView:nil andView:self andSuccessBlock:^(id responseObject) {
        [_dic setObject:imageStr forKey:@"headImg"];
        
        BaseModel *dataModel = [BaseModel mj_objectWithKeyValues:responseObject];
        if ([dataModel.status isEqualToString:@"200"]) {
            [NSKeyedArchiver archiveRootObject:_dic toFile:_userModelFile];
            _imageView.image=image;
        }
        else{
            [ZQTools svpInfo:dataModel.msg];
        }
        
        
    } anderrorBlock:nil];
    
    
}

@end
